/*
 * Created on Jun 24, 2005
 */
package org.vectrics.recruiter.job.event;

import java.util.HashSet;
import java.util.Iterator;
import java.util.Locale;
import java.util.Set;

import org.apache.log4j.Logger;
import org.vectrics.SystemException;
import org.vectrics.company.Company;
import org.vectrics.domain.Domain;
import org.vectrics.event.Event;
import org.vectrics.event.EventAttachment;
import org.vectrics.event.EventEmailBuilder;
import org.vectrics.event.EventHandler;
import org.vectrics.provider.CoreServiceLocator;
import org.vectrics.recruiter.RecruitServiceLocator;
import org.vectrics.recruiter.job.Job;
import org.vectrics.recruiter.job.JobCandidate;
import org.vectrics.user.PersonSearchCriteria;

/**
 * @author MMoore
 */
public class JobCandidateEventHandler extends EventHandler {
	private transient Logger log = Logger.getLogger(this.getClass());
	
	/**
	 * Constructor 
	 */
	public JobCandidateEventHandler() {
		super();
	}
	
	protected EventEmailBuilder getEmailBuilder(Event event, Locale locale) {
		return(new JobCandidateEmailBuilder(event, this.getJobCandidate(), locale));
	}
	
	protected void doHandleEvent(Event event) {
		log.debug("doHandleEvent(Event event) called");
	}

	protected final JobCandidate getJobCandidate() {
		if (this.getEvent().getAttachments().size() == 0) {
			throw new SystemException("Could not find job attached to job event - event: " + this.getEvent().getDescription());
		}
		Iterator iter = this.getEvent().getAttachments().iterator();
		EventAttachment attachment = (EventAttachment)iter.next();
		JobCandidate jobCandidate = RecruitServiceLocator.getJobProvider().findJobCandidateById(new Long(attachment.getObjectId()));
		return(jobCandidate);
	}

	
	
	protected Set findNotifyPersons() {
		Set notifyPersons = new HashSet();
		PersonSearchCriteria criteria = CoreServiceLocator.createPersonSearchCriteria();
		Job job = this.getJobCandidate().getJob();
		criteria.addDomain(job.getDomain());
		criteria.addPrivilege("job.edit.notify");
		notifyPersons.addAll(CoreServiceLocator.getPersonService().findPersons(criteria, 0, 1000));

		if (job.getPrimaryRecruiter() != null)
			notifyPersons.add(job.getPrimaryRecruiter());
		if (job.getSecondaryRecruiter() != null)
			notifyPersons.add(job.getSecondaryRecruiter());
		if (job.getHiringManager() != null)
			notifyPersons.add(job.getHiringManager());
		
		return(notifyPersons);
	}

}
